package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ApartmentLabelService labelService;
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private GraphInfoMapper graphInfoM;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private ApartmentInfoMapper mapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);

        //删除图片  配套    标签    杂费
        if (isUpdate) {

            //图图
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            lambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);

            //配套
            LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            facilityService.remove(lambdaQueryWrapper1);

            //标签
            LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            labelService.remove(lambdaQueryWrapper2);

            //杂费
            LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            feeValueService.remove(lambdaQueryWrapper3);
        }
        //插入 图片   配套  标签  杂费

        //图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            for (GraphInfo graphInfo : graphInfos) {

            }
            graphInfoService.saveBatch(graphInfos);
        }

        //配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds(); //配套id集合
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> facilityArrayList = new ArrayList<>();//公寓和配套的关系实体对象集合
            for (Long id : facilityInfoIds) {
//         实体类加了@builder是不可以new对象的
//                ApartmentFacility apartmentFacility = new ApartmentFacility();
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(id).build();
                facilityArrayList.add(build);
            }
            facilityService.saveBatch(facilityArrayList);
        }

        //标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> list = new ArrayList<>();
            for (Long id : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(id).build();
                list.add(build);
            }
            labelService.saveBatch(list);
        }
        //杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            List<ApartmentFeeValue> list = new ArrayList<>();
            for (Long id : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(id).build();
                list.add(build);
            }
            feeValueService.saveBatch(list);
        }


    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return mapper.pageApartmentItemByQuery(page, queryVo);
    }

    //回显方法
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
//查询apinfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }


        //查询图片
        List<GraphVo> graphVoList = graphInfoM.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //查询标签
        List<LabelInfo> labelInfos = labelInfoMapper.selectListByApartmentId(id);
        //配套
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectListByApartmentId(id);
        //杂费
        List<FeeValueVo> feeValueVos = feeValueMapper.selectListByApartmentId(id);
        ApartmentDetailVo detailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, detailVo);
        detailVo.setGraphVoList(graphVoList);
        detailVo.setLabelInfoList(labelInfos);
        detailVo.setFacilityInfoList(facilityInfos);
        detailVo.setFeeValueVoList(feeValueVos);

        return detailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(RoomInfo::getApartmentId, id);
        Long l = roomInfoMapper.selectCount(lambdaQueryWrapper3);
        if (l > 0) {
throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        this.removeById(id);
        //删除图片
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        queryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(queryWrapper);
        //删除标签
        LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        labelService.remove(lambdaQueryWrapper);
        //配套
        LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId, id);
        facilityService.remove(lambdaQueryWrapper1);
        //杂费
        LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(ApartmentFeeValue::getApartmentId, id);
        feeValueService.remove(lambdaQueryWrapper2);
    }
}




